<?php
	//Calculate page generation time
	$time = microtime();
	$time = explode(' ', $time);
	$time = $time[1] + $time[0];
	$start = $time;

	error_reporting(E_ALL);
	session_start();
	include("conf/config.conf.php");
	include("inc/database.inc.php"); 
	include("inc/general.inc.php");	
	include("inc/query1.inc.php");
	include("inc/query2.inc.php"); 
	include("inc/functions.inc.php"); 
	include("inc/map.inc.php");
	include("inc/login.inc.php"); 
	include("inc/home.inc.php"); 
	include("inc/admin.inc.php"); 
	
	$database_link;
	databaseConnect(); // connect to the database
	
	if(!isset($_SESSION['level'])) {
		$_SESSION['level'] = 0;
	}
	
	
	//display header & navigation
	displayHeader(); 
	displayNavigation();
	
	// Functions which generate output
	switch(getCurrentAction()) {
		case "loginCheck":
			if($_SESSION['level'] == 0 || $_SESSION['level'] == 1) {
				loginCheck();
			}else {
				displayLogin();
			}
		break;
		case "selectquery":
			if($_SESSION['level'] > 0) {
				selectquery();
			} else {
				displayLogin();}
		break;
		case "displayForgotPassword":
			displayForgotPassword();
		break;	
		case "newPassword":
			newPassword();
		break;		
		case "modifyUser":
			if($_SESSION['level'] > 1) {
				modifyUser();
			}else {displayLogin();}
			break;
		case "addUser":
			if($_SESSION['level'] > 1) {
				addUser();
			}else {displayLogin();}
			break;
		case "confirmationAddUser":
			if($_SESSION['level'] > 1) {
				confirmationAddUser();
			}else {displayLogin();}
			break;
		case "updateUser":
			if($_SESSION['level'] > 1) {
				updateUser();
			}else {displayLogin();}
			break;
		case "deleteUser":
			if($_SESSION['level'] > 1) {
				deleteUser();
			}else {displayLogin();}
			break;
		case "admin":
			if($_SESSION['level'] > 1) {
				tableOfUsers();
			}else {displayLogin();}
			break;
		case "stationsQuery1":
			if($_SESSION['level'] > 0) {
			stationsQuery1();
			}else {displayLogin();}
			break;
		case "stationDetails1":
			if($_SESSION['level'] > 0) {
				stationDetails1();
			}else {displayLogin();}
			break;
		case "stationsQuery2":
			if($_SESSION['level'] > 0) {
				stationsQuery2();
			}else {displayLogin();}
			break;
		case "stationDetails2":
			if($_SESSION['level'] > 0) {
				stationDetails2();
			}else {displayLogin();}
			
			break;
		case "map":
			if($_SESSION['level'] > 0) {
				displayMap();
			}else {displayLogin();}
			break;				
		case "logout":
				logout();
			break;
		case "displayChangePassword":
			if($_SESSION['level'] > 0) {
				displayChangePassword();
			} else {displayLogin();}
		break;
		case "changePassword":
			if($_SESSION['level'] > 0) {
				changePassword();
			} else {displayLogin();}
		break;
		case "home";
			if ($_SESSION['level'] > 0) {
				echo '<h2>Welcome '.$_SESSION['username'].'!</h2>';
			} else if ($_SESSION['level'] == 0) {
				displayLogin();
			}
		break;
		default:
			displayLogin();
		break;	
	}

		
	
	displayFooter(); // show the HTML footer
	
	databaseDisconnect(); // disconnect from database
	
	//Calculate page generation time
	$time = microtime();
	$time = explode(' ', $time);
	$time = $time[1] + $time[0];
	$finish = $time;
	$total_time = round(($finish - $start), 4);
	echo 'Page generated in '.$total_time.' seconds.';

?>